import type { NextApiRequest, NextApiResponse } from 'next';
import { BeError, BeSuccess } from '@/utils/response';
import saveStory from '@/services/tapd/saveStory';
import log from '@/utils/log';
import getConfig from 'next/config';

export default async function handler(req: NextApiRequest, res: NextApiResponse) {
  if (req.method?.toLocaleLowerCase() !== 'post') {
    res.status(200).json(BeError('不支持请求类型'));
    res.end();
    return;
  }
  const { event } = req.body;
  if (!event) {
    log.error('输入参数为空');
    res.status(500).json(BeError('输入参数为空'));
    return;
  }
  try {
    const result = await saveStory(event);
    res.status(200).json(BeSuccess(result));
  } catch (error: any) {
    log.error('tapd需求：', JSON.stringify(event));
    const { publicRuntimeConfig }: { publicRuntimeConfig: EnvParamsType } = getConfig();
    log.error('当前环境配置：', JSON.stringify(publicRuntimeConfig));
    log.error(error);
    res.status(500).json(BeError(error));
  }
}
